<?php

/**
 * skill actions.
 *
 * @package    jiazhang
 * @subpackage skill
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class skillActions extends sfActions
{
    /**
    * Executes index action
    *
    * @param sfRequest $request A request object
    */
    public function executeIndex(sfWebRequest $request)
    {
        $this->forward('default', 'module');
    }
    
    public function executeDelete(sfWebRequest $request){
        $id = (int)$request->getParameter('id',0);
        $skill = Doctrine::getTable('Skill')->findOneById($id);
        $return_msg = array('error'=>0,'msg'=>'未知状态');
        
        if($skill){
//            $skill->setStatus(3)->save();
            $skill->delete();
            $return_msg['msg'] = '删除成功！';
        }else{
            $return_msg['error'] = 1;
            $return_msg['msg'] = '数据不存在！';
        }
        
        
        return $this->renderText(json_encode($return_msg));
    }
    
    
    public function executeRecommend(sfWebRequest $request){
        
        
        $result = array();
        //广告
        $result['adList'] = array();
        $ad_list = Doctrine::getTable('SkillAd')->createQuery()->where('display = 1')->orderBy('sort_num desc')->limit(5)->execute();
        $i=0;
        foreach($ad_list as $ad){
            $result['adList'][$i]['id'] = $ad->getId();
            $result['adList'][$i]['picture'] = $ad->getImgUrl();
            $result['adList'][$i]['url'] = $ad->getUrl(); //存的是技能或文章ID
            $i++;
        }
        
        //专题
        $result['hortPopicList'] = array();
        $albums = Doctrine::getTable('SkillAlbum')->createQuery()->where('display = 1')->execute();
        $z=0;
        foreach($albums as $album){
            $result['hortPopicList'][$z]['id'] = $album->getId();
            $result['hortPopicList'][$z]['title'] = $album->getTitle();
            $result['hortPopicList'][$z]['picture'] = $album->getCover();
            $z++;
        }
        
        $page = (int)$request->getParameter('page',1);
        //推荐技能
        $result['ideaList'] = array();
        $dql = Doctrine::getTable('Skill')->createQuery()->where('status = 2')->orderBy('created_at desc');
        $this->pager = new sfDoctrinePager('Skill',30);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
        $s=0;
        foreach($this->pager->getResults() as $skill){
            
            $result['ideaList'][$s]['id'] = $skill->getId();
            $result['ideaList'][$s]['title'] = $skill->getTitle();
            $result['ideaList'][$s]['picture'] = $skill->getCover();
            $result['ideaList'][$s]['type'] = Skill::$category_label[$skill->getCategory()];
            $result['ideaList'][$s]['tag'] = array();
            
            $tag = Doctrine::getTable('SkillTag')->createQuery()->where('skill_id = ?',$skill->getId())->execute()->toKeyValueArray('tag_name', 'tag_name');
            if(count($tag) > 0){
                foreach($tag as $key => $value){
                    $result['ideaList'][$s]['tag'][] = $value;                    
                }
            }
            
            $result['ideaList'][$s]['likes'] = $skill->getStatistical()->getLikeNum();
            $result['ideaList'][$s]['max_age'] = $skill->getMaxAge();
            $result['ideaList'][$s]['min_age'] = $skill->getMinAge();
            $result['ideaList'][$s]['person'] = array();
            $result['ideaList'][$s]['person']['id'] = $skill->getUser()->getId();
            $result['ideaList'][$s]['person']['name'] = $skill->getUser()->getNickname();
            $result['ideaList'][$s]['person']['icon'] = $skill->getUser()->getCover();
            $s++;
        }
        
        return $this->renderText(json_encode($result));
    }
    
    public function executeFavorite_list(sfWebRequest $request){
          $id = (int)$request->getParameter('person_id','');
          
          $skills = Doctrine::getTable('SkillFavoriteRecord')->findByUserId($id);
          $result['IdeaList'] = array();
          $i=0;
          
          if(count($skills) > 0){

              foreach($skills as $key=>$skill){
                  $result['IdeaList'][$key]['id'] = $skill->getSkill()->getId();
                  $result['IdeaList'][$key]['title'] = $skill->getSkill()->getTitle();
                  $result['IdeaList'][$key]['picture'] = $skill->getSkill()->getCover();
                  $result['IdeaList'][$key]['type'] = Skill::$category_label[$skill->getSkill()->getCategory()];
                  $result['IdeaList'][$key]['tag'] = array();
                  
                  $tag = Doctrine::getTable('SkillTag')->createQuery()->where('skill_id = ?',$skill->getSkillId())->execute()->toKeyValueArray('tag_name', 'tag_name');
                    if(count($tag) > 0){
                        foreach($tag as $tkey => $value){
                            $result['IdeaList'][$key]['tag'][] = $value;                    
                        }
                    }
                  
                  $result['IdeaList'][$key]['likes'] =  $skill->getSkill()->getStatistical()->getLikeNum();
                  $result['IdeaList'][$key]['max_age'] = $skill->getSkill()->getMaxAge();
                  $result['IdeaList'][$key]['min_age'] = $skill->getSkill()->getMinAge();
                  $result['IdeaList'][$key]['person'] = array();
                  $result['IdeaList'][$key]['person']['id'] = $skill->getSkill()->getUser()->getId();
                  $result['IdeaList'][$key]['person']['name'] = $skill->getSkill()->getUser()->getNickname();
                  $result['IdeaList'][$key]['person']['icon'] = $skill->getSkill()->getUser()->getCover();
                  $i++;
              }
              
          }
          
          return $this->renderText(json_encode($result));
     }
    
     public function executeAlbum_show(sfWebRequest $request){
          $id = (int)$request->getParameter('hotPopic_id','');
          $album = Doctrine::getTable('SkillAlbum')->findOneById($id);
          $result = array();
          $result['hortPopic'] = array();
        
          if($album){
            $result['hortPopic']['id'] = $album->getId();
                    $result['hortPopic']['title'] = $album->getTitle();
                    $result['hortPopic']['picture'] = $album->getCover();
              $skill_ids = Doctrine::getTable('SkillAlbumIds')->findBySkillAlbumId($id);
              $result['IdeaList'] = array();
              
              foreach($skill_ids as $skill){
                  $result['IdeaList']['id'] = $skill->getSkill()->getId();
                  $result['IdeaList']['title'] = $skill->getSkill()->getTitle();
                  $result['IdeaList']['picture'] = $skill->getSkill()->getCover();
                  $result['IdeaList']['type'] = Skill::$category_label[$skill->getSkill()->getCategory()];
                  $result['IdeaList']['tag'] = array();
                  
                  $tag = Doctrine::getTable('SkillTag')->createQuery()->where('skill_id = ?',$skill->getSkillId())->execute()->toKeyValueArray('tag_name', 'tag_name');
                    if(count($tag) > 0){
                        foreach($tag as $key => $value){
                            $result['IdeaList']['tag'][] = $value;                    
                        }
                    }
                  
                  $result['IdeaList']['likes'] =  $skill->getSkill()->getStatistical()->getLikeNum();
                  $result['IdeaList']['max_age'] = $skill->getSkill()->getMaxAge();
                  $result['IdeaList']['min_age'] = $skill->getSkill()->getMinAge();
                  $result['IdeaList']['person'] = array();
                  $result['IdeaList']['person']['id'] = $skill->getSkill()->getUser()->getId();
                  $result['IdeaList']['person']['name'] = $skill->getSkill()->getUser()->getNickname();
                  $result['IdeaList']['person']['icon'] = $skill->getSkill()->getUser()->getCover();
                  
              }
              
          }
          
          return $this->renderText(json_encode($result));
     }
    
    public function executeAlbum_list(sfWebRequest $request){
        $albums = Doctrine::getTable('SkillAlbum')->createQuery()->where('display = 1')->execute();
        $result = array();
        $result['hortPopicList'] = array();
        
        foreach($albums as $album){
            $result['hortPopicList']['id'] = $album->getId();
                    $result['hortPopicList']['title'] = $album->getTitle();
                    $result['hortPopicList']['picture'] = $album->getCover();
        }
        
        return $this->renderText(json_encode($result));
    }
    
    
    public function executeCreate_skill(sfWebRequest $request){
        sfConfig::set('sf_web_debug',false);
        if($request->isMethod('POST')){
            $json_obj = new Services_JSON();
            $msg = array('error'=>0,'msg'=>'技能创建成功');
            //主表参数
            $title = $request->getParameter('title','');//技能名称
            $cover = $request->getParameter('picture','');//封面
            $max_age = (int)$request->getParameter('max_age',0);
            $min_age = (int)$request->getParameter('min_age',0);
            $category = (int)$request->getParameter('type',0);//分类
            $user_id = (int)$request->getParameter('person_id',0);//用户ID
            //添加主表
            $skill_model = new Skill();
            $skill_model->setTitle($title)
                            ->setCover($cover)
                            ->setMaxAge($max_age)
                            ->setMinAge($min_age)
                            ->setCategory($category)
                            ->setUserId($user_id)
                            ->save();
            $skill_id = $skill_model->getId();
            
            
            
            if($skill_id == 0){
                $msg['error'] = 1;
                $msg['msg']   = '技能创建失败';
                return $this->renderText(json_encode($msg));
            }
            //添加技能统计总表
            $skill_statistical_model = new SkillStatistical();
            $skill_statistical_model->setSkillId($skill_id)->save();
            $skill_statistical_id = $skill_statistical_model->getId();
            //添加技能标签表
            $tags = json_decode($request->getParameter('tags','[]'),true);
            if(is_array($tags) && count($tags) > 0){
                foreach($tags as $key => $value){
                    $skill_tag_model = new SkillTag();
                    $skill_tag_model->setSkillId($skill_id)->setTagName($value)->save();
                }
            }

            //添加步骤
            $steps = json_decode($request->getParameter('steps','[]'),true);
            if(is_array($steps) && count($steps) > 0){
                $step_num = 0;//技能步骤数
                $skill_photo_num = 0;//技能总图片数
                foreach ($steps as $key => $value){
                    $step_photo_num = 0;//步骤图片数
                    //添加步骤主表
                    $step_model = new Step();
                    $step_model->setSkillId($skill_id)->setSortNum($value['sort_num'])->setTitle($value['title'])->save();
                    $step_id = $step_model->getId();

                    //添加步骤统计表
                    $step_statistical_model = new StepStatistical();
                    $step_statistical_model->setStepId($step_id)->save();
                    $step_statistical_id = $step_statistical_model->getId();
                    //添加步骤中的块
                    if(isset($value['contents']) && is_array($value['contents']) && count($value['contents']) > 0){
                        foreach($value['contents'] as $key_one => $value_one){
                            //如果是图片则加1
                            if(strlen($value_one['picture']) > 0){
                                $step_photo_num += 1;
                            }
                            //添加块主表
                            $step_block_model = new StepBlock();
                            $step_block_model->setImgUrl($value_one['picture'])
                                                              ->setText($value_one['specification'])
                                                              ->setSortNum($value_one['sort_num'])
                                                              ->setStepId($step_id)
                                                              ->setSkillId($skill_id)
                                                              ->save();
                            $step_block_id = $step_block_model->getId();
                            //添加块统计
                            $block_statistical_model = new BlockStatistical();
                            $block_statistical_model->setStepId($step_id)->setBlockId($step_block_id)->save();
                        }
                    }
                    //更新步骤统计表中的图片数量
                    $step_statistical = Doctrine::getTable('StepStatistical')->findOneById($step_statistical_id);
                    $step_statistical->setPhotoNum($step_photo_num)->save();
                            
                    //技能步骤数
                    $step_num += 1;
                    //技能总图片数
                    $skill_photo_num += $step_photo_num;
                }
                //更新技能总计总表中的步骤数量
                $skill_statistical =  Doctrine::getTable('SkillStatistical')->findOneById($skill_statistical_id);
                $skill_statistical->setStepNum($step_num)->setPhotoNum($skill_photo_num)->save();
            }
            //添加成功
            
            return $this->renderText(json_encode($msg));
        }
    }
  
    public function executeGet_skill(sfWebRequest $request){
        sfConfig::set('sf_web_debug',false);
            $skill_data = array();
            $skill_id = (int)$request->getParameter('idea_id',0);
            $person_id = (int)$request->getParameter('person_id',0);
            //获取技能主表数据
            $skill = Doctrine::getTable('Skill')->findOneById($skill_id);
            
            if(!$skill){
                return $this->renderText(json_encode(array()));
            }
            
            $skill_data['idea']['id'] = $skill->getId();
            $skill_data['idea']['picture'] = $skill->getCover();
            $skill_data['idea']['type'] = $skill->getCategory();
            $skill_data['idea']['min_age'] = $skill->getMinAge();
            $skill_data['idea']['max_age'] = $skill->getMaxAge();
            $skill_data['idea']['title'] = $skill->getTitle();
            $skill_data['idea']['title'] = $skill->getTitle();
            $skill_data['idea']['createTime'] = $skill->getCreatedAt();
            
            #判断是否已经收藏
            $is_favoriter = Doctrine::getTable('SkillFavoriteRecord')->createQuery()->where('user_id = ?',$person_id)->andWhere('skill_id = ?',$skill_id)->count();
            
//            if($is_favoriter > 0){
            $skill_data['idea']['isCollection'] = $is_favoriter;
//            }
            
            $skill_data['idea']['tag'] = array();
            $skill_data['idea']['steps'] = array();
            
            //获取技能标签
            $tag = Doctrine::getTable('SkillTag')->createQuery()->where('skill_id = ?',$skill->getId())->execute()->toKeyValueArray('tag_name', 'tag_name');
            if(count($tag) > 0){
                foreach($tag as $key => $value){
                    $skill_data['idea']['tag'][] = $value;                    
                }
            }
            
            //获取技能统计总表
            $skill_statistical =  $skill->getStatistical();
            //Doctrine::getTable('SkillStatistical')->findOneById($skill->getId());
            $skill_data['idea']['step_num'] = $skill_statistical->getStepNum();
            $skill_data['idea']['pictureNum'] = $skill_statistical->getPhotoNum();
            
            //获取技能步骤
            $steps = Doctrine::getTable('Step')->createQuery()->where('skill_id = ?',$skill->getId())->orderBy('sort_num')->fetchArray();
            foreach($steps as $key => $step){
                $skill_data['idea']['steps'][$key]['id'] = $step['id'];
                $skill_data['idea']['steps'][$key]['sort_num'] = $step['sort_num'];
                $skill_data['idea']['steps'][$key]['title'] = $step['title'];
                $skill_data['idea']['steps'][$key]['contents'] = array();
                //获取步骤内容
                $contents = Doctrine::getTable('StepBlock')->createQuery()->where('step_id = ?',$step['id'])->orderBy('sort_num')->fetchArray();
                foreach($contents as $key_one => $content){
                    $skill_data['idea']['steps'][$key]['contents'][$key_one]['id'] = $content['id'];
                    $skill_data['idea']['steps'][$key]['contents'][$key_one]['specification'] = $content['text'];
                    $skill_data['idea']['steps'][$key]['contents'][$key_one]['picture'] = $content['img_url'];
                    $skill_data['idea']['steps'][$key]['contents'][$key_one]['sort_num'] = $content['sort_num'];
                    
                    //获取当前块统计
                    $block_statistical = Doctrine::getTable('BlockStatistical')->findOneByBlockId($content['id']);
                    $skill_data['idea']['steps'][$key]['contents'][$key_one]['likes'] = $block_statistical->getLikeNum();
                    $skill_data['idea']['steps'][$key]['contents'][$key_one]['comments'] = $block_statistical->getCommentNum();
                    
                    //获取当前块的评论
                    $skill_data['idea']['steps'][$key]['contents'][$key_one]['commentList'] = array();
                    $comments = Doctrine::getTable('BlockComment')->createQuery()->where('block_id = ?',$content['id'])->fetchArray();
                    foreach($comments as $key_two => $comment){
                        $skill_data['idea']['steps'][$key]['contents'][$key_one]['commentList'][$key_two]['id'] = $comment['id'];
                        $skill_data['idea']['steps'][$key]['contents'][$key_one]['commentList'][$key_two]['content'] = $comment['content'];
                        $skill_data['idea']['steps'][$key]['contents'][$key_one]['commentList'][$key_two]['commentTime'] = $comment['created_at'];
                        $skill_data['idea']['steps'][$key]['contents'][$key_one]['commentList'][$key_two]['person'] = array();
                        $user = Doctrine::getTable('User')->findOneById($comment['user_id']);
                        $skill_data['idea']['steps'][$key]['contents'][$key_one]['commentList'][$key_two]['person']['id'] = $user->getId();
                        $skill_data['idea']['steps'][$key]['contents'][$key_one]['commentList'][$key_two]['person']['name'] = $user->getNickname();
                        $skill_data['idea']['steps'][$key]['contents'][$key_one]['commentList'][$key_two]['person']['icon'] = $user->getCover();
                    }
                }
            }
            
            //获取技能发布者信息
            $user = Doctrine::getTable('User')->findOneById($skill->getUserId());
            $skill_data['person']['id'] = $user->getId();
            $skill_data['person']['name'] = $user->getNickname();
            $skill_data['person']['icon'] = $user->getCover();
            
            //返回数据
            return $this->renderText(json_encode($skill_data));
    }    
    
     //收藏
  public function executeFavorite(sfWebRequest $request){
      sfConfig::set('sf_web_debug',false);
      
      if($request->isMethod('POST')){
          $msg = array('error'=>0,'msg'=>'');
          $user_id = (int)$request->getParameter('person_id',0);
          $skill_id = (int)$request->getParameter('idea_id',0);
          $user = Doctrine::getTable('User')->findOneById($user_id);
          $skill = Doctrine::getTable('Skill')->findOneById($skill_id);
          
          if($user && $skill){
              
              if($skill->getUserId() == $user_id){
                $msg['error'] = 1;
                $msg['msg'] = '您不能收藏自己的技能哦！';
              }
              
              $has_fav = Doctrine::getTable('SkillFavoriteRecord')->createQuery()->where('user_id = ?',$user_id)->andWhere('skill_id = ?',$skill_id)->execute();
              
              if(count($has_fav) > 0){
                $has_fav->delete();
                $skill->decreaseFavoritesNum();
                $msg['error'] = 0;
                $msg['msg'] = '取消收藏成功'; 
              }else{
                $skill_favorite = new SkillFavoriteRecord();
                $skill_favorite->setUserId($user_id)->setSkillId($skill_id)->save();
                $skill->addFavoritesNum();
                $msg['error'] = 0;
                $msg['msg'] = '恭喜您，收藏成功！'; 
              }
              
          }else{
              $msg['error'] = 1;
              $msg['msg'] = '数据不存在！请检查';
          }
          
      }
      
      return $this->renderText(json_encode($msg));
  }
  
  //喜欢
  public function executeLike(sfWebRequest $request){
      sfConfig::set('sf_web_debug',false);
      $msg = array('error'=>0,'msg'=>'');
      
      if($request->isMethod('POST')){
          $user_id = (int)$request->getParameter('person_id',0);
          $skill_id = (int)$request->getParameter('idea_id',0);
          $block_id = (int)$request->getParameter('content_id',0);
          $block = Doctrine::getTable('StepBlock')->findOneById($block_id);
          $skill = Doctrine::getTable('Skill')->findOneById($skill_id);
          $step_id = $block->getStepId();
          $step = $block->getStep();
          
          $has_fav = Doctrine::getTable('BlockLikeRecord')->createQuery()->where('user_id = ?',$user_id)->andWhere('block_id = ?',$block_id)->execute();
              
          if(count($has_fav) > 0){
              
            $has_fav->delete();
            $block->decreasetLikeNum();
            $step->decreasetLikeNum();
            $skill->decreasetLikeNum();
            $msg['error'] = 0;
            $msg['msg'] = '取消喜欢！'; 
          }else{
          
            $block_like_record = new BlockLikeRecord();
            $block_like_record->setStepId($step_id)->setBlockId($block_id)->setUserId($user_id)->save();
            $block->addLikeNum();
            $step->addLikeNum();
            $skill->addLikeNum();
            $msg['error'] = 0;
            $msg['msg'] = '增加喜欢！'; 
         }
          
      }
      
     return $this->renderText(json_encode($msg));
  }
    
    //用户技能列表
    public function executeSkill_list($request) {
        sfConfig::set('sf_web_debug',false);
        $person_id = (int)$request->getParameter('person_id',0);

        //获取用户技能列表
        $skill_list = Doctrine::getTable('Skill')->createQuery()->where('user_id = ?',$person_id)->andWhere('status <> 3')->orderBy('id desc')->fetchArray();
        $data = array();
        $data['ideaList'] = array(); 
        if(count($skill_list) > 0){
            foreach($skill_list as $key => $value){
                $data['ideaList'][$key]['id'] = $value['id'];
                $data['ideaList'][$key]['title'] = $value['title'];
                $data['ideaList'][$key]['picture'] = '';
                if(strlen($value['cover']) > 0){
                    $data['ideaList'][$key]['picture'] = $value['cover'];
                }else{
                    $step_block = Doctrine::getTable('StepBlock')->createQuery()->where('skill_id = ?',$value['id'])->andWhere('img_url != ?', '')->fetchOne();
                    if($step_block){
                        $data['ideaList'][$key]['picture'] = $step_block['img_url'];
                    }
                }
                $data['ideaList'][$key]['type'] = $value['category'];
                $data['ideaList'][$key]['max_age'] = $value['max_age'];
                $data['ideaList'][$key]['min_age'] = $value['min_age'];

                //获取当前技能统计数据
                $skill_statistical = Doctrine::getTable('SkillStatistical')->findOneBy('skill_id', $value['id']);
                $data['ideaList'][$key]['likes'] = $skill_statistical->getLikeNum();

                //获取当前技能标签
                $data['ideaList'][$key]['tag'] = array();
                $skill_tag = Doctrine::getTable('SkillTag')->createQuery()->where('skill_id = ?',$value['id'])->fetchArray();
                foreach($skill_tag as $key_one => $tag){
                    $data['ideaList'][$key]['tag'][$key_one] = $tag['tag_name'];
                }
            }
        }
        return $this->renderText(json_encode($data));
    }  
  
    //技能分类列表
    public function executeSkill_type($request) {
        sfConfig::set('sf_web_debug',false);
        $person_id = (int)$request->getParameter('person_id',0);
        $type = (int)$request->getParameter('type',0);
        $area = $request->getParameter('area','');

        //获取分类技能列表
        $skill_list = Doctrine::getTable('Skill')->createQuery()->where('category = ?',$type)->andWhere('status >= 1')->orderBy('id desc')->fetchArray();
        $data = array();
        $data['ideaList'] = array(); 
        if(count($skill_list) > 0){
            foreach($skill_list as $key => $value){
                $data['ideaList'][$key]['id'] = $value['id'];
                $data['ideaList'][$key]['title'] = $value['title'];
                $data['ideaList'][$key]['picture'] = '';
                if(strlen($value['cover']) > 0){
                    $data['ideaList'][$key]['picture'] = $value['cover'];
                }else{
                    $step_block = Doctrine::getTable('StepBlock')->createQuery()->where('skill_id = ?',$value['id'])->andWhere('img_url != ?', '')->fetchOne();
                    if($step_block){
                        $data['ideaList'][$key]['picture'] = $step_block['img_url'];
                    }
                }
                $data['ideaList'][$key]['type'] = $value['category'];
                $data['ideaList'][$key]['max_age'] = $value['max_age'];
                $data['ideaList'][$key]['min_age'] = $value['min_age'];

                //获取当前技能统计数据
                $skill_statistical = Doctrine::getTable('SkillStatistical')->findOneBy('skill_id', $value['id']);
                $data['ideaList'][$key]['likes'] = $skill_statistical->getLikeNum();

                //获取当前技能标签
                $data['ideaList'][$key]['tag'] = array();
                $skill_tag = Doctrine::getTable('SkillTag')->createQuery()->where('skill_id = ?',$value['id'])->fetchArray();
                foreach($skill_tag as $key_one => $tag){
                    $data['ideaList'][$key]['tag'][$key_one] = $tag['tag_name'];
                }
                
                //获取用户信息
                $user = Doctrine::getTable('User')->findOneById($value['user_id']);
                $data['ideaList'][$key]['person'] = array();
                $data['ideaList'][$key]['person']['id'] = $user->getId();
                $data['ideaList'][$key]['person']['name'] = $user->getNickname();
                $data['ideaList'][$key]['person']['icon'] = $user->getCover();
            }
        }
        return $this->renderText(json_encode($data));
    }  
    
      public function executeCreate_comment(sfWebRequest $request){
      sfConfig::set('sf_web_debug',false);
      $msg = array('error'=>0,'msg'=>'');
      
      if($request->isMethod('POST')){
          $user_id = (int)$request->getParameter('person_id',0);
          $skill_id = (int)$request->getParameter('idea_id',0);
          $block_id = (int)$request->getParameter('content_id',0);
          $content = $request->getParameter('content','');
          $parent_id = (int)$request->getParameter('reply_id',0);
          
          $block = Doctrine::getTable('StepBlock')->findOneById($block_id);
          $skill = Doctrine::getTable('Skill')->findOneById($skill_id);

          $step_id = $block->getStepId();
          $step = $block->getStep();
          $reply_user_id = 0;
          
          if($parent_id > 0){
              $reply_comment = Doctrine::getTable('BlockComment')->findOneById($parent_id);
              if($reply_comment){
                  $reply_user_id = $reply_comment->getUserId();
              }
          }
          
          $comment = new BlockComment();
          $comment->setStepId($step_id)->setBlockId($block_id)->setContent($content)->setParentId($parent_id)->setUserId($user_id)->setReplayUserId($reply_user_id)->save();
          
          $block->addCommentNum();
          $step->addCommentNum();
          $skill->addCommentNum();
          $msg['error'] = 0;
          $msg['msg'] = '评论发布成功';
      }
      return $this->renderText(json_encode($msg));
  }
    
}
